<?php

namespace frame\lib;

class VerfiyParam
{
    // 参数名称
    protected static $name;
    // 参数别称 username -> 用户名
    protected static $paramName;
    // 参数数组
    protected static $data;
    // 数值
    protected static $val;
    // 条件
    protected static $rule;

    /**
     * @param Array $data 需要被检查的
     * @param Array $rule 检查规则 （跟tp框架类型)
     */
    public static function init($data, $rule)
    {
        self::$data = $data;
        foreach ($rule as $k => $v) {
            $nick = explode('|', $k);
            $paramName = $nick[1] ?? $k;
            self::$paramName = $paramName;
            $name = $nick[0];
            static::$name = $name;
            $condition = explode('|', $v);
            foreach ($condition as $m => $n) {
                $aa = explode(':', $n);
                $type = $aa[0] ?? $n;
                $val = $aa[1] ?? '';
                self::$rule = $val;
                $value = @$data[$name];
                self::$val = $value;
                if(!$value && !in_array('require' , $condition))
                {
                    continue;
                }
                self::$type($paramName);
            }
        }
    }
    public static function require($paramName)
    {
        if (!array_key_exists(@self::$name, self::$data)) error("[$paramName]不能为空" , 201);
        if(empty(self::$val)) error("[$paramName]不能为空" , 201);
    }
    public static function min($paramName)
    {
        $rule = self::$rule;
        if (strlen(self::$val) < $rule) error("[$paramName]最小值为$rule");
    }
    public static function max($paramName)
    {
        $rule = self::$rule;
        if (strlen(self::$val) > $rule)  error("[$paramName]最大值为$rule");
    }
    public static function int($paramName)
    {
        if (!is_numeric(self::$val)) error("[$paramName]类型非整形");
    }
    public static function email($paramName)
    {
        if (!filter_var(self::$val,FILTER_VALIDATE_EMAIL)) error("[$paramName]非邮箱格式");
    }
    public static function alphaNum($paramName)
    {
        if (preg_match_all('/^[\p{L}\d]+$/u', self::$val) && !preg_match('/[\x{4e00}-\x{9fa5}]/u', self::$val)) {
            return true;
        } else {
            error("[$paramName]非字母和数字结合");
        }
    }
    public static function in($paramName)
    {
        $limit = self::$rule;
        $arrge = explode(',',$limit);
        if (!in_array(self::$val, $arrge))  error("[$paramName]不在范围 $limit 中");
    }
    public static function lt($paramName)
    {
        $rule = self::$rule;
        if (self::$val >= $rule)  error("[$paramName]数值应小于 $rule");
    }
    public static function gt($paramName)
    {
        $rule = self::$rule;
        if (self::$val <= $rule)  error("[$paramName]数值应大于 $rule");
    }
    public static function elt($paramName)
    {
        $rule = self::$rule;
        if (self::$val > $rule)  error("[$paramName]数值应小于或等于 $rule");
    }
    public static function egt($paramName)
    {
        $rule = self::$rule;
        if (self::$val < $rule)  error("[$paramName]数值应大于或等于 $rule");
    }
}
